var express = require('express');
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");

var router = express.Router();
const vipsdb = require("../db/vipsdb");


router.post('/', function (req, res, next) {

  // 1、接收前端传来的数据
  let username = req.body.username;
  let userpass = req.body.userpass;

  console.log("username",username);
  console.log("userpass",userpass);

  // 2、连接数据库，查询
  vipsdb.find({
    username: username,
    userpass: userpass
  },
    (data) => {
      if (data.length > 0) {
        
        // 产生token
        let token = jwt.sign(username, "what is your name");
    
        res.json({
          status: "success",
          token
        })
      } else {
        res.json({
          status: "fail",
          message: "用户名或者密码不对"
        });
      }
    },
    () => {
      res.json({
        status: "fail",
        message: "数据库连接或者操作有问题"
      });
    });
});

module.exports = router;
